package com.losdun.product.controller;

import com.losdun.common.annotation.Log;
import com.losdun.common.core.controller.BaseController;
import com.losdun.common.core.domain.AjaxResult;
import com.losdun.common.core.page.TableDataInfo;
import com.losdun.common.enums.BusinessType;
import com.losdun.common.utils.poi.ExcelUtil;
import com.losdun.product.domain.Parts;
import com.losdun.product.domain.vo.PartsOptionsVo;
import com.losdun.product.service.IPartsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 产品配件Controller
 * 
 * @author sword.ye
 * @date 2025-03-02
 */
@RestController
@RequestMapping("/product/parts")
public class PartsController extends BaseController
{
    @Autowired
    private IPartsService partsService;

    /**
     * 查询产品配件列表
     */
    @PreAuthorize("@ss.hasPermi('product:parts:list')")
    @GetMapping("/list")
    public TableDataInfo list(Parts parts)
    {
        startPage();
        List<Parts> list = partsService.selectPartsList(parts);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('product:parts:list')")
    @GetMapping("/getPartsOptions")
    public AjaxResult list()
    {
        List<PartsOptionsVo> list = partsService.selectPartsOptionsVoList();
        return success(list);
    }

    /**
     * 导出产品配件列表
     */
    @PreAuthorize("@ss.hasPermi('product:parts:export')")
    @Log(title = "产品配件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Parts parts)
    {
        List<Parts> list = partsService.selectPartsList(parts);
        ExcelUtil<Parts> util = new ExcelUtil<Parts>(Parts.class);
        util.exportExcel(response, list, "产品配件数据");
    }

    /**
     * 获取产品配件详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:parts:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(partsService.selectPartsById(id));
    }

    /**
     * 新增产品配件
     */
    @PreAuthorize("@ss.hasPermi('product:parts:add')")
    @Log(title = "产品配件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Parts parts)
    {
        return toAjax(partsService.insertParts(parts));
    }

    /**
     * 修改产品配件
     */
    @PreAuthorize("@ss.hasPermi('product:parts:edit')")
    @Log(title = "产品配件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Parts parts)
    {
        return toAjax(partsService.updateParts(parts));
    }

    /**
     * 删除产品配件
     */
    @PreAuthorize("@ss.hasPermi('product:parts:remove')")
    @Log(title = "产品配件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(partsService.deletePartsByIds(ids));
    }
}
